﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Microsoft.Practices.Unity;

namespace Core.Presenters
{
    class BuyingPresenter
    {
        private IBuilding _building;
        private ICustomer _customer;
        private IOwnedBuilding _iBuildingl;
        private List<ICustomer> _customers;
        private IBuyingView _view;
        private ICustomerDAO _customerDAO;
        private double _endprice;
        private IOwnedBuildingDAO _ownedBuildingDAO;
        private IOwnedBuilding _ownedBuilding;
        private IAction _action;


        public BuyingPresenter(IBuilding building,IAction action, IBuyingView view)
        {
            _action = action;
            _building = building;
            _view = view;
            _customerDAO = ResolverContainer.Resolver.Resolve<ICustomerDAO>();
            _ownedBuilding = ResolverContainer.Resolver.Resolve<IOwnedBuilding>();
            _ownedBuildingDAO = ResolverContainer.Resolver.Resolve<IOwnedBuildingDAO>();
            _customers = _customerDAO.GetAll(false);
            _view.BuyNow += Save;
            _view.Building.DataSource = _building;
            _view.Customer.DataSource = _customers;
            double n = 1.05;
            if ((_building.StartPrice > 15000000) && (_building.StartPrice < 25000000))
            {
                n = 1.07;
            }
            else
            {
                if ((_building.StartPrice > 25000000) && (_building.StartPrice < 50000000))
                {
                    n = 1.1;
                }
                else
                {
                    n = 1.12;  
                }
            }
            _endprice = _building.StartPrice*1.05*n;
            _view.EndPrice = _endprice.ToString();
            _view.ShowDialog();
        }

        private void Save(object sender,EventArgs eventArgs)
        {
            _building = (IBuilding) _view.Building.Current;
            _customer = (ICustomer) _view.Customer.Current;
            _ownedBuilding.Action = _action;
            _ownedBuilding.Building = _building;
            _ownedBuilding.Customer = _customer;
            _ownedBuilding.DayOfBuy = _action.Date;
            _ownedBuilding.EndPrice = (int) _endprice;

            if (_ownedBuildingDAO.SaveOrUpdateAndCommit(_ownedBuilding))
            {
                _view.Close();
            }
        }
    }
}
